package cn.ubeta.b2c.user.order;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import cn.ubeta.b2c.R;
import cn.ubeta.b2c.api.ApiOrder;
import cn.ubeta.b2c.bean.LogisticsInfo;
import cn.ubeta.b2c.common.AnalyseActivity;
import cn.ubeta.b2c.utils.DateTimeUtils;
import cn.ubeta.b2c.utils.NetworkCallback;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by liudengyong on 2016/9/7.
 */
public class LogisticsActivity extends AnalyseActivity {

    private static final String PARAM_ORDER_ID = "order_id";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_logistics);
        long orderId = getIntent().getLongExtra(PARAM_ORDER_ID, -1L);
        getLogistics(orderId);
    }

    /**
     * 获取物流信息
     *
     * @param orderId
     */
    private void getLogistics(long orderId) {
        ApiOrder.getLogistics(orderId, new NetworkCallback<LogisticsInfo>() {
            @Override
            public void onResponse(LogisticsInfo info) {
                final LinearLayout container = (LinearLayout) findViewById(R.id.ll_logistics_items);
                final TextView tvOrderId = (TextView) findViewById(R.id.tv_order_id);
                final TextView tvTime = (TextView) findViewById(R.id.tv_order_time);
                final TextView tvComp = (TextView) findViewById(R.id.tv_order_logistics_comp);
                SimpleDraweeView ivLogisticsLogo = (SimpleDraweeView) findViewById(R.id.img_logistics_logo);

                tvOrderId.setText(DateTimeUtils.format("%d", info.getOrderId()));
                // 时间
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                tvTime.setText(dateFormat.format(new Date(info.getTime())));

                // 物流公司名称
                tvComp.setText(info.getLogisticsName());
                // 设置物流logo
                if (!TextUtils.isEmpty(info.getLogisticsLogoUrl())) {
                    ivLogisticsLogo.setImageURI(Uri.parse(info.getLogisticsLogoUrl()));
                }

                // 物流详情
                List<LogisticsInfo.StepInfo> items = info.getStepInfos();
                for (int i = 0; i < items.size(); i++) {
                    LogisticsInfo.StepInfo item = items.get(i);
                    View view = getLayoutInflater().inflate(R.layout.item_logistics, null);
                    ((TextView) view.findViewById(R.id.tv_item_detail)).setText(item.getMessage());
                    Date date = new Date(item.getTime());
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                    ((TextView) view.findViewById(R.id.tv_item_time)).setText(format.format(date));
                    container.addView(view);
                }
            }

            @Override
            public void onFailure(IOException e) {
            }
        });
    }

    /**
     * 跳转到查看物流
     * @param context
     * @param orderId
     */
    public static void gotoLogistics(Context context, long orderId) {
        Intent intent = new Intent(context, LogisticsActivity.class);
        intent.putExtra(PARAM_ORDER_ID, orderId);
        context.startActivity(intent);
    }

}
